﻿// test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "LogicSystem.h"
#include <csignal>
#include <thread>
#include <mutex>
#include "AsioIOServicePool.h"
#include "CServer.h"
#include "ConfigMgr.h"
#include "Logger.h"
#include "RedisMgr.h"
#include "interface/INetworkDelivery.h"
#include<crtdbg.h>
using namespace std;

int main()
{
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF);
	// 指定日志文件路径）
	Logger::Init("server_logs/log_%N.log"); // %N 会自动替换为序号（滚动日志）
	// 开发环境设置为 Debug 级别，生产环境设为 Info
	Logger::SetLevel(boost::log::trivial::debug);
	LOG_INFO << "Server started successfully!";

	auto& cfg = ConfigMgr::Inst();
	auto server_name = cfg["SelfServer"]["Name"];
	try {
		std::string value = "";
		RedisMgr::GetInstance()->Get("blogwebsite", value);
		std::cout << value << std::endl;
		auto pool = AsioIOServicePool::GetInstance();
		boost::asio::io_context  io_context;
		auto port_str = cfg["SelfServer"]["Port"];
		short port = static_cast<unsigned short>(atoi(port_str.c_str()));
		
		boost::asio::signal_set signals(io_context, SIGINT, SIGTERM);
		signals.async_wait([&io_context, pool](auto, auto) {
			io_context.stop();
			pool->Stop();
			});
		CServer s(io_context, port);

		LogicSystem::GetInstance()->Init();
		//DeliveryFactory::GetInstance().Init(DeliveryType::LOGIC_SYSTEM);

		// 投递到Kafka
		 DeliveryFactory::GetInstance().Init(
		     DeliveryType::KAFKA, 
		     "localhost:9092", 
			 "comm_to_biz_topic",               // 通信层→业务层：生产主题
			 "biz_to_comm_topic"
		 );
		 s.InitKafka();
		io_context.run();
	}
	catch (std::exception& e) {
		LOG_ERROR << "Exception: " << e.what() << endl;
	}
}
